#绘制多个条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
plt.rcParams['font.family'] = 'SimHei'
#导入字体

a = ['2018','2019','2020','2021']
beijing = [14.08,16.42,16.76,18.4]
shanghai = [13.5,15.71,15.94,17.38]
guangzhou = [14.286,15.9,13.4,15.12]
shenzhen = [15.532,20.7,15.76,15.2]
nanjing = [15.2886,16.5681,15.9083,17.4520]
wuhan = [13.5877,14.5545,12.6687,13.5251]
#定义变量
bar_width = 0.1

bar_1 = list(range(len(a)))
bar_2 = [i+bar_width for i in bar_1]
bar_3 = [i+bar_width for i in bar_2]
bar_4 = [i+bar_width for i in bar_3]
bar_5 = [i+bar_width for i in bar_4]
bar_6 = [i+bar_width for i in bar_5]
#设置图片尺寸与清晰度
plt.figure(figsize=(20, 8), dpi=80)

#导入数据，绘制条形图
plt.bar(range(len(a)), beijing, width=bar_width, label='上海')
plt.bar(bar_2, shanghai, width=bar_width, label='北京')
plt.bar(bar_3, guangzhou, width=bar_width, label='广州')
plt.bar(bar_4, shenzhen, width=bar_width, label='深圳')
plt.bar(bar_5, nanjing, width=bar_width, label='南京')
plt.bar(bar_6, wuhan, width=bar_width, label='武汉')
#添加标题
plt.title('2018-2019各城市人均GDP', size=20)
#添加xy轴
plt.xlabel('年份')
plt.ylabel('人均GDP')
#x轴刻度
plt.xticks(bar_2, a, size=15)
plt.legend()

#展示效果图
plt.show()
